#include "loader.h"
#include <QTextStream>
#include <QStringList>
#include <iostream>
Loader::Loader(QString filename, GeometryData *data)
{
    filename_ = filename;
    data_ = data;
}

bool isComment(QString &line)
{
    return line.startsWith('#');
}

QString getLine(QTextStream &in)
{
    QString line;
    while (!in.atEnd())
    {
        line = in.readLine();
        if( isComment(line) )
            continue;
        if (line.size() == 0)
            continue;
        break;
    }

    return line;
}

void Loader::load()
{
    printf("Got line: \n");
    QFile file(filename_);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        setErrorMessage("Can't open file");
        emit finished();
        return;
    }

    QTextStream in(&file);

    //get file data type (first line not comment)
    int data_type = -1;

    QString line = getLine(in);

    data_type = line.toInt();

   bool ret;
   if (data_type == 2 || data_type == 4 )
   {
       ret = triangleLoader(data_, in);
       data_->data_type = GeometryData::TRIANGLES;
   }

   if (data_type == 0 )
   {
       ret = pointsLoader(data_, in);
       data_->data_type = GeometryData::POINTS;
   }

   if (data_type == 1 )
   {
       ret = contourLoader(data_, in);
       data_->data_type = GeometryData::CONTOURS;
   }

   emit finished();
   return;
}

bool Loader::contourLoader(GeometryData *data,  QTextStream &in)
{
    //read point count (first non comment line)
    int progress_prev = 0;
    int contours_count = -1;

    QString line = getLine(in);

    contours_count = line.toInt();
    std::cout << "Ct: "<<contours_count<<std::endl;
    if (contours_count <= 0)
        return false;

    for (int i = 0; i< contours_count; i++)
    {
        int point_count = -1;

        line = getLine(in);
        QStringList linelist = line.split(" ",QString::SkipEmptyParts);

        point_count = linelist.at(0).toInt();
        double param = linelist.at(1).toInt();

        if (point_count <= 0)
            return false;

        Contour *c = data->startContour(param);
        for (int k = 0; k < point_count; k++)
        {
            line = getLine(in);
            QStringList linelist = line.split(" ",QString::SkipEmptyParts);

            double x = linelist.at(0).toDouble();
            double y = linelist.at(1).toDouble();
            double param = 0;
            if (linelist.count() == 3)
                param = linelist.at(2).toDouble();
            c->addPoint(data->addPoint(x,y,param));
         }


        int progress =  ((double)i / (double)contours_count) * 100;
        if (progress > progress_prev)
        {
            emit progressChanged(progress, "Loading points");
            progress_prev = progress;
        }
    }
}

bool Loader::pointsLoader(GeometryData *data,  QTextStream &in)
{
    //read point count (first non comment line)
    int progress_prev = 0;

    int point_count = -1;

    QString line = getLine(in);
    point_count = line.toInt();

    if (point_count <= 0)
        return false;
    data->allocPoints(point_count);

    for (int i = 0; i < point_count; i++)
    {
        line = getLine(in);
        QStringList linelist = line.split(" ",QString::SkipEmptyParts);

        double x = linelist.at(0).toDouble();
        double y = linelist.at(1).toDouble();
        double param = 0;
        if (linelist.count() == 3)
            param = linelist.at(2).toDouble();

        data->addPoint(x,y,param);

        int progress =  ((double)i / (double)point_count) * 100;
        if (progress > progress_prev)
        {
            emit progressChanged(progress, "Loading points");
            progress_prev = progress;
        }
    }

}

bool Loader::triangleLoader(GeometryData *data,  QTextStream &in)
{
    //read point count (first non comment line)
    pointsLoader(data, in);

    int progress_prev = 0;

    int point_count = -1;

    int triangle_count = -1;
    int triangle_attrib_count = 3;

    QString line = getLine(in);
    QStringList linelist = line.split(" ",QString::SkipEmptyParts);

    triangle_count = linelist.at(0).toInt();

    if (linelist.size() >= 2)
        triangle_attrib_count = linelist.at(1).toInt();

    data->allocTriangles(point_count);

    progress_prev = 0;
    for (int  i = 0; i <  triangle_count; i++)
    {
        line = getLine(in);

        QStringList linelist = line.split(" ",QString::SkipEmptyParts);

        int p1 = linelist.at(0).toInt()-1;
        int p2 = linelist.at(1).toInt()-1;
        int p3 = linelist.at(2).toInt()-1;
        int param = 0;

        if (linelist.count() ==  4)
            param = linelist.at(3).toInt();

        if (linelist.count() ==  7)
            param = linelist.at(6).toInt();


        data->addTriangle(p1, p2, p3, param);
        int progress =  ((double)i / (double)triangle_count) * 100;
        if (progress > progress_prev)
        {
            emit progressChanged(progress,"Loading triangles");
            progress_prev = progress;
        }
    }

return true;
}
